package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import net.sf.json.JSONSerializer; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.Date; 
import java.util.List; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXABienNhanHoSo;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoService;
import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyen;
import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyenService;

import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoGiayTo;

import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoGiayToService;

import com.lb.jwitter.service.TblQLDTTXADMGiayToService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXABienNhanHoSoUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    private int page; 
    public int getPage() { return page; }

    private int pages; 
    public int getPages() { return pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private String ghiChu; 
    public void setGhiChu(String ghiChu) {
       this.ghiChu = ghiChu;
 }

    public String getGhiChu() {
       return ghiChu;
 }

    private String ngayNhan; 
    public void setNgayNhan(String ngayNhan) {
       this.ngayNhan = ngayNhan;
 }

    public String getNgayNhan() {
       return ngayNhan;
 }

    private String soBienLai; 
    public void setSoBienLai(String soBienLai) {
       this.soBienLai = soBienLai;
 }

    public String getSoBienLai() {
       return soBienLai;
 }

    private int canBoId; 
    public void setCanBoId(int canBoId) {
       this.canBoId = canBoId;
 }

    public int getCanBoId() {
       return canBoId;
 }

    private int hoSoId; 
    public void setHoSoId(int hoSoId) {
       this.hoSoId = hoSoId;
 }

    public int getHoSoId() {
       return hoSoId;
 }

    private TblQLDTTXABienNhanHoSo tblQLDTTXABienNhanHoSo; 
    public void setTblQLDTTXABienNhanHoSo (TblQLDTTXABienNhanHoSo tblQLDTTXABienNhanHoSo) {
        this.tblQLDTTXABienNhanHoSo = tblQLDTTXABienNhanHoSo;
 }

    private Collection<TblQLDTTXABienNhanHoSo> tblQLDTTXABienNhanHoSos; 
    public void setTblQLDTTXABienNhanHoSos(Collection<TblQLDTTXABienNhanHoSo> tblQLDTTXABienNhanHoSos) {
        this.tblQLDTTXABienNhanHoSos = tblQLDTTXABienNhanHoSos; 
      } 

    private TblQLDTTXABienNhanHoSoService tblQLDTTXABienNhanHoSoService; 

    private TblCanBoService tblCanBoService; 

    private TblQLDTTXAHoSoXetTuyenService tblQLDTTXAHoSoXetTuyenService; 

    private String tblQLDTTXABienNhanHoSoGiayToStr; 
    public void setTblQLDTTXABienNhanHoSoGiayToStr (String tblQLDTTXABienNhanHoSoGiayToStr) 
 {     this.tblQLDTTXABienNhanHoSoGiayToStr = tblQLDTTXABienNhanHoSoGiayToStr;
 }

    public String getTblQLDTTXABienNhanHoSoGiayToStr() {
       return tblQLDTTXABienNhanHoSoGiayToStr;
 }

    private TblQLDTTXABienNhanHoSoGiayToService tblQLDTTXABienNhanHoSoGiayToService; 

    private TblQLDTTXADMGiayToService tblQLDTTXADMGiayToService; 

	private int preUpdate(Message msg, TblQLDTTXABienNhanHoSo obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblQLDTTXABienNhanHoSo obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_BienNhanHoSo", "sua")){ 
        return "noprivilege"; 
    }        tblQLDTTXABienNhanHoSoService = (TblQLDTTXABienNhanHoSoService) context.getBean("tbl_QLDTTXA_BienNhanHoSoClient"); 
        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 

        tblQLDTTXAHoSoXetTuyenService = (TblQLDTTXAHoSoXetTuyenService) context.getBean("tbl_QLDTTXA_HoSoXetTuyenClient"); 

        tblQLDTTXABienNhanHoSoGiayToService = (TblQLDTTXABienNhanHoSoGiayToService) context.getBean("tbl_QLDTTXA_BienNhanHoSo_GiayToClient"); 

        tblQLDTTXADMGiayToService = (TblQLDTTXADMGiayToService) context.getBean("tbl_QLDTTXA_DM_GiayToClient"); 

        msg = new Message(); 
        tblQLDTTXABienNhanHoSo = tblQLDTTXABienNhanHoSoService.gettblQLDTTXABienNhanHoSo(new Long(id)); 
        tblQLDTTXABienNhanHoSo.setGhiChu(ghiChu); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayNhan); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblQLDTTXABienNhanHoSo.setNgayNhan(d); 
        tblQLDTTXABienNhanHoSo.setSoBienLai(soBienLai); 
        tblQLDTTXABienNhanHoSo.setCanBoId(tblCanBoService.gettblCanBo(new Long(canBoId))); 
        tblQLDTTXABienNhanHoSo.setHoSoId(tblQLDTTXAHoSoXetTuyenService.gettblQLDTTXAHoSoXetTuyen(new Long(hoSoId))); 
        preUpdate(msg,tblQLDTTXABienNhanHoSo); 
       if (msg.isPreMessage_b()) 
       { 
           tblQLDTTXABienNhanHoSoService.updatetblQLDTTXABienNhanHoSo(tblQLDTTXABienNhanHoSo); 
           postUpdate(msg, tblQLDTTXABienNhanHoSo); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

    List < ConditionReport > cons = new ArrayList<ConditionReport>(); 
    ConditionReport con = new ConditionReport(); 
    con.setCol("bienNhanId.id"); 
    con.setValue1(tblQLDTTXABienNhanHoSo.getId()); 
    con.setOperator("eq"); 
    cons.add(con); 
    List<TblQLDTTXABienNhanHoSoGiayTo> tblQLDTTXABienNhanHoSoGiayToList = tblQLDTTXABienNhanHoSoGiayToService.findtblQLDTTXABienNhanHoSoGiayToByCondition(cons, 0, 0); 
    if(tblQLDTTXABienNhanHoSoGiayToList != null && tblQLDTTXABienNhanHoSoGiayToList.size() >0){ 
         List<String> ids = new ArrayList<String>();  
         for(TblQLDTTXABienNhanHoSoGiayTo obj0 : tblQLDTTXABienNhanHoSoGiayToList ){ 
             Collections.addAll(ids, obj0.getId().toString());  
         } 
         tblQLDTTXABienNhanHoSoGiayToService.deleteListtblQLDTTXABienNhanHoSoGiayTo(ids);  
     }
    JSONArray jsonTblQLDTTXABienNhanHoSoGiayTo = (JSONArray)JSONSerializer.toJSON(tblQLDTTXABienNhanHoSoGiayToStr); 
    for (int i = 0; i < jsonTblQLDTTXABienNhanHoSoGiayTo.size(); i++) 
     { 
         JSONObject jsObj = jsonTblQLDTTXABienNhanHoSoGiayTo.getJSONObject(i); 
         TblQLDTTXABienNhanHoSoGiayTo obj0 = new TblQLDTTXABienNhanHoSoGiayTo(); 
        obj0.setGiayToId(tblQLDTTXADMGiayToService.gettblQLDTTXADMGiayTo(new Long(jsObj.getInt("giayToId")))); 
       obj0.setBienNhanId(tblQLDTTXABienNhanHoSo); 
       tblQLDTTXABienNhanHoSoGiayToService.createtblQLDTTXABienNhanHoSoGiayTo(obj0); 
   } 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_QLDTTXA_BienNhanHoSo Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
